package box
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class CompositeBox extends Sprite
	{
		private var world:b2World;
		public static const RATIO:Number = 30;
		
		public function CompositeBox()
		{
			setupWorld();
			
			setupBody(new b2Vec2(4,4));
			addEventListener(Event.ENTER_FRAME,loop);
		}
		private function setupWorld():void{
			world = new b2World(new b2Vec2(0,10),true);
			
			var debugDraw:b2DebugDraw = new b2DebugDraw();
				debugDraw.SetSprite(this);
				debugDraw.SetAlpha(0.6);
				debugDraw.SetDrawScale(RATIO);
				debugDraw.SetFillAlpha(0.4);
				debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
				debugDraw.SetLineThickness(2);
			world.SetDebugDraw(debugDraw);
		}
		private function setupBody(pos:b2Vec2):void{
			var bodyDef:b2BodyDef = new b2BodyDef();
				bodyDef.position = pos;
				
			var polyShape:b2PolygonShape = new b2PolygonShape();
				polyShape.SetAsBox(2,2);
			var circleShape:b2CircleShape = new b2CircleShape(3);
				
			var body:b2Body = world.CreateBody(bodyDef);
			var pos1:b2Vec2 = body.GetLocalPoint(new b2Vec2(pos.x+2,pos.y));
				circleShape.SetLocalPosition(pos1);
				body.CreateFixture2(polyShape,0);
				body.CreateFixture2(circleShape,0);
		}
		
		private function loop(e:Event):void{
			world.Step(1/30,10,10);
			world.DrawDebugData();
		}
		
	}
}